package com.base.util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * 线程池初始化(推荐)
 * 初始化的线程池无需关闭
 *
 * @author cjq
 */
@Component
public class BlockPoolExecutor {

    /**
     * 日志
     */
    private static final Logger logger = LoggerFactory.getLogger(BlockPoolExecutor.class);
    /**
     * 核心线程池大小
     */
    private static int corePoolSize;
    /**
     * 最大线程池大小
     */
    private static int maximumPoolSize;
    /**
     * 线程池中超过corePoolSize数目的空闲线程的最大存活时间，单位根据TimeUnit
     */
    private static long keepAliveTime;
    /**
     * 阻塞队列大小
     */
    private static int blockQueue;
    /**
     * 用户自定义线程池
     */
    private static ThreadPoolExecutor udPool = null;

    static {
        logger.error("BlockPoolExecutor.static 初始化线程池");
        InputStream in = null;
        in = BlockPoolExecutor.class.getClassLoader().getResourceAsStream("application.yml");
        Properties properties = new Properties();
        try {
            properties.load(in);
            String udCorePoolSize = properties.getProperty("corePoolSize", "30");
            String udMaximumPoolSize = properties.getProperty("maximumPoolSize", "50");
            String udKeepAliveTime = properties.getProperty("keepAliveTime", "30");
            String udBlockQueue = properties.getProperty("blockQueue", "30");
            if (udCorePoolSize != null && udCorePoolSize.trim().length() > 0) {
                corePoolSize = Integer.valueOf(udCorePoolSize);
            }
            if (udMaximumPoolSize != null && udMaximumPoolSize.trim().length() > 0) {
                maximumPoolSize = Integer.valueOf(udMaximumPoolSize);
            }
            if (udKeepAliveTime != null && udKeepAliveTime.trim().length() > 0) {
                keepAliveTime = Integer.valueOf(udKeepAliveTime);
            }
            if (udBlockQueue != null && udBlockQueue.trim().length() > 0) {
                blockQueue = Integer.valueOf(udBlockQueue);
            }

        } catch (IOException e) {
            e.printStackTrace();
            logger.error("BlockPoolExecutor.static.IOException 初始化线程池失败:" + e.getMessage());
        }
        udPool = new ThreadPoolExecutor(
                corePoolSize,
                maximumPoolSize,
                keepAliveTime,
                TimeUnit.MINUTES,
                new ArrayBlockingQueue<Runnable>(blockQueue)
        );
    }

    /**
     * 关闭线程池
     */
    public static void destroy() {
        if (udPool != null) {
            udPool.shutdown();
        }
    }

    /**
     * 获取线程池
     *
     * @return
     */
    public static ExecutorService getThreadPool() {
        if(!udPool.isShutdown()){
            return udPool;
        }else{
            udPool = new ThreadPoolExecutor(
                    corePoolSize,
                    maximumPoolSize,
                    keepAliveTime,
                    TimeUnit.MINUTES,
                    new ArrayBlockingQueue<Runnable>(blockQueue));
        }
        return udPool;
    }


}
